package com.AndroidApp.codememo.databse;

import com.AndroidApp.codememo.model.Codememo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CodeMemoDbAdapter {
	 // Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CATEGORY = "category";
    public static final String KEY_TITLE = "title";
    public static final String KEY_UNAME = "uname_accno";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_OTHRDETAILS = "othrdetails";
    private static final String DATABASE_TABLE = "memo";
    private Context context;
    private SQLiteDatabase database;
    private CodeMemoDatabaseHelper dbHelper;

    public CodeMemoDbAdapter(Context context) {

        this.context = context;
    }

    //opens the database via the CodeMemoDatabaseHelper class
    public CodeMemoDbAdapter open() throws SQLException {

        dbHelper = new CodeMemoDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {

        dbHelper.close();
    }

    /**
         * Create a new todo If the todo is successfully created return the new
         * rowId for that note, otherwise return a -1 to indicate failure.
         */

    public long createMemo(Codememo item) {

        ContentValues initialValues = createContentValues(item);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Update the Codememo
         */

    public boolean updateCodememo(long rowId, Codememo item) {

        ContentValues updateValues = createContentValues(item);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Deletes Codememo
         */

    public boolean deleteCodememo(long rowId) {

        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Return a Cursor over the list of all Codememo in the database
         * 
         * @return Cursor over all notes
         */

    public Cursor fetchAllCodememos() {

        return database.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_TITLE, KEY_UNAME, KEY_PASSWORD, KEY_OTHRDETAILS 
        }, null, null, null, null, null);
    }

    /**
         * Return a Cursor positioned at the defined Codememo
         */

    public Cursor fetchCodememo(long rowId) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_CATEGORY, KEY_TITLE, KEY_UNAME, KEY_PASSWORD, KEY_OTHRDETAILS
        }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues(Codememo items) {

        ContentValues values = new ContentValues();
        values.put(KEY_CATEGORY, items.category);
        values.put(KEY_TITLE, items.title);
        values.put(KEY_UNAME, items.uname_accno);
        values.put(KEY_PASSWORD, items.password);
        values.put(KEY_OTHRDETAILS, items.othrdetails);
        return values;
    }
}
